CuPy
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
top
CuPy is an open source library for GPU-accelerated computing with Python programming language, providing support for multi-dimensional arrays, sparse matrices, and a variety of numerical algorithms implemented on top of them.cite-ref-3[3] CuPy shares the same API set as NumPy and SciPy, allowing it to be a drop-in replacement to run NumPy/SciPy code on GPU. CuPy supports Nvidia CUDA GPU platform, and AMD ROCm GPU platform starting in v9.0.cite-ref-4[4]cite-ref-5[5]
CuPy is a NumFOCUS sponsored project.cite-ref-13[13]
Contents
• Features
• Examples
• See also
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Features
NumPy-compatible APIs
The same set of APIs defined in the NumPy package (numpy.*) are available under cupy.* package.
• Multi-dimensional array (cupy.ndarray) for boolean, integer, float, and complex data types
• Module-level functions
• Linear algebra functions
SciPy-compatible APIs
The same set of APIs defined in the SciPy package (scipy.*) are available under cupyx.scipy.* package.
• Sparse matrices (cupyx.scipy.sparse.*_matrix) of CSR, COO, CSC, and DIA format
• Advanced linear algebra
• Multidimensional image processing
• Sparse linear algebra
• Special functions
User-defined GPU kernels
• Kernel templates for element-wise and reduction operations
• Raw kernel (CUDA C/C++)
• Just-in-time transpiler (JIT)
• Kernel fusion
Distributed computing
• Distributed communication package (cupyx.distributed), providing collective and peer-to-peer primitives
Low-level CUDA features
Interoperability
• DLPackcite-ref-17[17]
• CUDA Array Interfacecite-ref-18[18]
• NEP 13 (__array_ufunc__)cite-ref-19[19]
Examples
Array creation
>>> import cupy as cp
>>> x = cp.array([1, 2, 3])
>>> x
array([1, 2, 3])
>>> y = cp.arange(10)
>>> y
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Basic operations
>>> import cupy as cp
>>> x = cp.arange(12).reshape(3, 4).astype(cp.float32)
>>> x
array([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]], dtype=float32)
>>> x.sum(axis=1)
array([ 6., 22., 38.], dtype=float32)
Raw CUDA C/C++ kernel
>>> import cupy as cp
>>> kern = cp.RawKernel(r'''
... extern "C" __global__
... void multiply_elemwise(const float* in1, const float* in2, float* out) {
... int tid = blockDim.x * blockIdx.x + threadIdx.x;
... out[tid] = in1[tid] * in2[tid];
... }
... ''', 'multiply_elemwise')
>>> in1 = cp.arange(16, dtype=cp.float32).reshape(4, 4)
>>> in2 = cp.arange(16, dtype=cp.float32).reshape(4, 4)
>>> out = cp.zeros((4, 4), dtype=cp.float32)
>>> kern((4,), (4,), (in1, in2, out)) # grid, block and arguments
>>> out
array([[ 0., 1., 4., 9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.],
[144., 169., 196., 225.]], dtype=float32)
Applications
See also
References
cite-note-11. "Release v1.3.0 – chainer/chainer". Retrieved 25 June 2022 – via GitHub.
cite-note-github-releases-22. "Releases – cupy/cupy". Retrieved 8 September 2024 – via GitHub.
cite-note-33. ↑ citerefokutaunnonishinohido2017Okuta, Ryosuke; Unno, Yuya; Nishino, Daisuke; Hido, Shohei; Loomis, Crissman (2017). CuPy: A NumPy-Compatible Library for NVIDIA GPU Calculations (PDF). Proceedings of Workshop on Machine Learning Systems (LearningSys) in The Thirty-first Annual Conference on Neural Information Processing Systems (NIPS).
cite-note-55. ↑ "AMD Leads High Performance Computing Towards Exascale and Beyond". 28 June 2021. Retrieved 21 June 2022. Most recently, CuPy, an open-source array library with Python, has expanded its traditional GPU support with the introduction of version 9.0 that now offers support for the ROCm stack for GPU-accelerated computing.
cite-note-66. ↑ "Preferred Networks released Version 2 of Chainer, an Open Source framework for Deep Learning - Preferred Networks, Inc". 2 June 2017. Retrieved 18 June 2022.
cite-note-77. ↑ "NumPy". numpy.org. Retrieved 21 June 2022.
cite-note-99. ↑ citerefoak-ridge-leadership-computing-facilityOak Ridge Leadership Computing Facility. "Installing CuPy". OLCF User Documentation. Retrieved 21 June 2022.
cite-note-1010. ↑ citerefnational-energy-research-scientific-computing-centerNational Energy Research Scientific Computing Center. "Using Python on Perlmutter". NERSC Documentation. Retrieved 21 June 2022.
cite-note-1111. ↑ citerefeth-zurichETH Zurich. "CuPy". ScientificComputing. Retrieved 21 June 2022.
cite-note-1212. ↑ citerefnational-institute-of-advanced-industrial-science-and-technologyNational Institute of Advanced Industrial Science and Technology. "Chainer". ABCI 2.0 User Guide. Retrieved 21 June 2022.
cite-note-1313. ↑ "Sponsored Projects - NumFOCUS". Retrieved 8 September 2024.
cite-note-1414. ↑ "Overview". CuPy documentation. Retrieved 18 June 2022.
cite-note-1515. ↑ "Comparison Table". CuPy documentation. Retrieved 18 June 2022.
cite-note-1616. ↑ "CUDA Python | NVIDIA Developer". Retrieved 21 June 2022.
cite-note-1717. ↑ "Welcome to DLPack's documentation!". DLPack 0.6.0 documentation. Retrieved 21 June 2022.
cite-note-1818. ↑ "CUDA Array Interface (Version 3)". Numba 0.55.2+0.g2298ad618.dirty-py3.7-linux-x86_64.egg documentation. Retrieved 21 June 2022.
cite-note-1919. ↑ "NEP 13 — A mechanism for overriding Ufuncs — NumPy Enhancement Proposals". numpy.org. Retrieved 21 June 2022.
cite-note-2020. ↑ "NEP 18 — A dispatch mechanism for NumPy's high level array functions — NumPy Enhancement Proposals". numpy.org. Retrieved 21 June 2022.
cite-note-2121. ↑ citerefcharles-r-harrisk-jarrod-millmanst-fan-j-van-der-waltralf-gommers2020Charles R Harris; K. Jarrod Millman; Stéfan J. van der Walt; et al. (16 September 2020). "Array programming with NumPy" (PDF). Nature. 585 (7825): 357–362. arXiv:2006.10256. doi:10.1038/S41586-020-2649-2. ISSN 1476-4687. PMC 7759461. PMID 32939066. Wikidata Q99413970.
cite-note-2222. ↑ "2021 report - Python Data APIs Consortium" (PDF). Retrieved 21 June 2022.
cite-note-2323. ↑ "Purpose and scope". Python array API standard 2021.12 documentation. Retrieved 21 June 2022.
cite-note-2424. ↑ "Install spaCy". spaCy Usage Documentation. Retrieved 21 June 2022.
cite-note-2626. ↑ "Python Package Introduction". xgboost 1.6.1 documentation. Retrieved 21 June 2022.
cite-note-2828. ↑ "Open GPU Data Science | RAPIDS". Retrieved 21 June 2022.
cite-note-2929. ↑ "API Docs". RAPIDS Docs. Retrieved 21 June 2022.
cite-note-3030. ↑ "Efficient Data Sharing between CuPy and RAPIDS". Retrieved 21 June 2022.
cite-note-3131. ↑ "10 Minutes to cuDF and CuPy". Retrieved 21 June 2022.
cite-note-3232. ↑ citerefalex2022Alex, Rogozhnikov (2022). Einops: Clear and Reliable Tensor Manipulations with Einstein-like Notation. International Conference on Learning Representations.
cite-note-3434. ↑ "Array API support (experimental) — scikit-learn documentation". Retrieved 8 September 2024.
cite-note-3535. ↑ citereftokuiokutaakibaniitani2019Tokui, Seiya; Okuta, Ryosuke; Akiba, Takuya; Niitani, Yusuke; Ogawa, Toru; Saito, Shunta; Suzuki, Shuji; Uenishi, Kota; Vogel, Brian; Vincent, Hiroyuki Yamazaki (2019). Chainer: A Deep Learning Framework for Accelerating the Research Cycle. Proceedings of the 25th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining. doi:10.1145/3292500.3330756.
External links